package org.tea.application.configuration;

import org.cef.CefApp;
import org.cef.CefClient;
import org.cef.browser.CefBrowser;
import org.cef.handler.CefLoadHandler;
import org.cef.handler.CefLoadHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tea.application.browser.MainFrame;
import org.tea.application.service.CookieService;

import java.util.List;

/**
 * @author lxh
 */
@Configuration
public class BrowserConfiguration {

    private final static Logger log = LoggerFactory.getLogger(BrowserConfiguration.class);

    private final static String[] ARGS = new String[]{"--disable-web-security"};

    @Bean(name = "teaMainFrame")
    public MainFrame mainFrame(CookieService cookieService, List<CefLoadHandlerAdapter> loadHandlerList) {
        if (!CefApp.startup(ARGS)) {
            throw new RuntimeException("Startup initialization failed!");
        }
        MainFrame mainFrame = new MainFrame(false,
                false, false,
                0, cookieService,
                loadHandlerList,
                ARGS
        );
        mainFrame.setSize(800, 700);
        mainFrame.setVisible(true);
        return mainFrame;
    }

    @Bean(name = "teaCefClient")
    public CefClient cefClient(MainFrame mainFrame) {
        return mainFrame.getClient_();
    }

    @Bean
    public CefBrowser teaCefBrowser(MainFrame mainFrame) {
        return mainFrame.getBrowser();
    }
}
